package com.app.teacherActivity.fragment;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.MainActivity;
import com.app.R;
import com.app.studentActivity.Password;
import com.app.studentActivity.Personal;
import com.app.studentActivity.Qingjia;
import com.app.util.DBHelper;

public class PersonFragment extends Fragment {

    private LinearLayout layout1, layout2, layout3;
    private Button btn;
    private TextView text1, text2;
    private int id;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_person2, container, false);
        id = getActivity().getIntent().getIntExtra("id", 0);
        initView(view);
        setData();
        text2.setText("学号:" + id);
        layout1.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), Personal.class);
            intent.putExtra("id", id);
            startActivity(intent);
        });
        layout2.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), Password.class);
            intent.putExtra("id", id);
            startActivity(intent);
        });
        layout3.setOnClickListener(v -> {
            Intent intent = new Intent(getActivity(), Qingjia.class);
            startActivity(intent);
        });
        btn.setOnClickListener(v -> {
            startActivity(new Intent(getContext(), MainActivity.class));
            getActivity().finish();
        });
        return view;
    }

    private void setData() {
        DBHelper dbHelper = new DBHelper(getContext());
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        String sqlQuery = "SELECT * FROM user WHERE id = ?";
        Cursor cursor = db.rawQuery(sqlQuery, new String[]{String.valueOf(id)});
        if (cursor.moveToFirst()) {
            String user_id = cursor.getString(cursor.getColumnIndex("user_id"));
            String username = cursor.getString(cursor.getColumnIndex("username"));
            text1.setText("姓名:" + username);
            text2.setText("职工号:" + user_id);
        }
        dbHelper.close();
        cursor.close();
    }

    private void initView(View view) {
        Toolbar toolbar = view.findViewById(R.id.toolbar);
        toolbar.getChildAt(0).setVisibility(View.GONE);
        ((TextView) toolbar.getChildAt(1)).setText("个人中心");
        layout1 = view.findViewById(R.id.layout1);
        layout2 = view.findViewById(R.id.layout2);
        layout3 = view.findViewById(R.id.layout3);
        btn = view.findViewById(R.id.btn);
        text1 = view.findViewById(R.id.text1);
        text2 = view.findViewById(R.id.text2);
    }

    @Override
    public void onResume() {
        super.onResume();
        // 每次返回到该页面时都更新数据
        setData();
    }
}